package schedule

import (
	"hy_808/protocol/hygprs"
)

/*
调度令

+0  A0 00 56 02 00 12 A6
+7  01 00 00 00 34 线路号
+12 02 00 00 06 4C 车号
+17 13 00 01 86 A1 调度员编号
+22 06 62 14 A6 82 unix时间
+27 14 62 14 A2 50 主站计划发车时间
+32 15 62 14 A8 2C 副站计划到达时间
+37 16 00 00 00 00 副站计划发车时间
+42 17 00 00 00 00 主站计划到达时间
+47 43 02 调度令类别，只支持7种类别：取消为0；主到主为1;主到副为2；副到主为3;主区到主为4；主区到副为5；副区到主为6；副到副7；副到副区 8
+49 0C 01 下行发车的开始站点
+51 0D 0C 下行发车的结束站点
+53 25 00 上行发车的开始站点
+55 26 00 上行发车的结束站点
+57 27 00 00 00 00 分支线路号
+62 18 00 当前调度令班次的序号，0x00时，表示最后一个班次
+64 23 00 15 30 44 24 00 15 30 44 19 00 00 1A 00 EF 00 后台所需数据
-5  00 00 0B 90 A1
*/

type Command struct {
	hygprs.Fields
}

func MakeCommand(p hygprs.Fields) Command {
	return Command{p}
}

func (p Command) MasterPlanDepartureTime() uint32 {
	return hygprs.ToUint32(p.Data[28:32])
}

func (p Command) AuxiliaryPlanArriveTime() uint32 {
	return hygprs.ToUint32(p.Data[33:37])
}

func (p Command) AuxiliaryPlanDepartureTime() uint32 {
	return hygprs.ToUint32(p.Data[38:42])
}

func (p Command) MasterPlanArriveTime() uint32 {
	return hygprs.ToUint32(p.Data[43:47])
}

/*
调度令类别
M = Master Station 主场
MS = Section Master Station 区间主站
A = Auxiliary Station 副场
AS = Section Auxiliary Station 区间副站
*/
type CommandType byte

const (
	CTCancel = 0
	CTM2M    = 1
	CTM2A    = 2
	CTA2M    = 3
	CTMS2M   = 4
	CTMS2A   = 5
	CTAS2M   = 6
	CTA2A    = 7
	CTA2AS   = 8
)

func (ct CommandType) String() string {
	switch ct {
	case CTCancel:
		return "取消"
	case CTM2M:
		return "主到主"
	case CTM2A:
		return "主到副"
	case CTA2M:
		return "副到主"
	case CTMS2M:
		return "主区到主"
	case CTMS2A:
		return "主区到副"
	case CTAS2M:
		return "副区到主"
	case CTA2A:
		return "副到副"
	case CTA2AS:
		return "副到副区"
	default:
		return "未定义"
	}
}

func (p Command) CommandType() CommandType {
	return CommandType(p.Data[48])
}

func (p Command) IsDown() bool {
	switch p.CommandType() {
	case CTM2M, CTM2A, CTMS2M, CTMS2A:
		return true
	default:
		return false
	}
}

func (p Command) DownBegin() byte {
	return p.Data[50]
}

func (p Command) DownEnd() byte {
	return p.Data[52]
}

func (p Command) UpBegin() byte {
	return p.Data[54]
}

func (p Command) UpEnd() byte {
	return p.Data[56]
}

func (p Command) BranchLineID() uint32 {
	return hygprs.ToUint32(p.Data[58:62])
}

func (p Command) CommandSN() byte {
	return p.Data[64]
}
